<template>
  <div class="order-container">
    <div class="search-bar">

      <el-button type="primary" style="margin-left: 200px"
                 @click="openNewTab('http://localhost:8080/finishProduct/getAllFinishProductVo')">查看bom图
      </el-button>
      <el-button type="warning" style="margin-left: 230px"
                 @click="router.push('/user')">返回首页
      </el-button>


      <el-text style="margin-left: 800px">你好呀,{{ nickname }}</el-text>

      <el-button type="danger" style="margin-left: 100px" @click="logout">退出登录</el-button>

    </div>

    <!-- 订单表格 -->
    <el-card shadow="never" class="table-card">
      <el-table
          :data="orderList"
          v-loading="loading"
          stripe
          style="width: 100%"
      >
        <el-table-column prop="order_no" label="订单号" width="220" />
        <el-table-column label="商品信息" min-width="280">
          <template #default="{ row }">
            <div class="product-list">
              <div v-for="(item, index) in row.items" :key="index" class="product-item">
                <div class="product-info">
                  <el-image
                      :src="item.image"
                      fit="cover"
                      class="product-image"
                      :preview-src-list="[item.image]"
                  />
                  <div class="product-detail">
                    <div class="product-name">{{ item.productId }}</div>
                    <div class="product-quantity">数量：{{ item.quantity }}</div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="提货信息" width="220">
          <template #default="{ row }">
            <div class="pickup-info">
              <div>申请人：{{ row.operator_id }}</div>

              <div>申请时间：{{ formatTime(row.created_at) }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120" align="center">
          <template #default="{ row }">
            <el-tag
                :type="statusStyle[row.status]"
                effect="dark"
            >
              {{ statusMap[row.status] }}
            </el-tag>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
            v-model:current-page="pagination.current"
            v-model:page-size="pagination.size"
            :total="pagination.total"
            :page-sizes="[10, 20, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted} from 'vue'
import {ElMessage} from "element-plus";
import {getAllOrdersInfoByUser, getUserNickName} from "@/api/auth.js";
import router from "@/router/index.js";
import {Search} from "@element-plus/icons-vue";

// 状态配置
const statusOptions = [
  { value: 'pending', label: '待审核' },
  { value: 'approved', label: '已通过' },
  { value: 'rejected', label: '已拒绝' }
]

const statusMap = {
  'pending': '待审核',
  'approved': '已通过',
  'rejected': '已拒绝'
}

const statusStyle = {
  'pending': 'warning',
  'approved': 'success',
  'rejected': 'danger'
}

// 查询表单
const queryForm = reactive({
  order_no: '',
  status: null
})

// 分页配置
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 模拟数据
const orderList = ref([
  {
    order_no: 'DD202307280001',
    status: 'approved',
    operator_id: '张三',
    created_at: 1690526400000,
    items: [
      {
        productId: 'P1001',
        quantity: 1,
        image: 'https://example.com/image1.jpg'
      },
      {
        productId: 'P1002',
        quantity: 2,
        image: 'https://example.com/image2.jpg'
      }
    ]
  },
  // 更多模拟数据...
])
const initData=async ()=>{
  try {
    const res=await getAllOrdersInfoByUser()
    if(res.code===200){
      orderList.value=res.data
    }else{
      ElMessage.error(res.message)
    }
  }catch (e){
    ElMessage.error(e)
  }
  try {
    const res=await getUserNickName()
    if(res.code===200){
      nickname.value=res.data
    }else{
      ElMessage.error(res.message)
    }
  }catch (e){
    ElMessage.error(e)
  }

}
const openNewTab = (url) => {
  window.open(url, '_blank', 'noopener,noreferrer')
}
onMounted(()=>{
  initData()
})

const loading = ref(false)

// 时间格式化
const formatTime = timestamp => {
  const date = new Date(timestamp)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
const nickname=ref('')

// 查询处理
const handleSearch = () => {
  // 这里应调用API接口
  console.log('查询参数:', JSON.parse(JSON.stringify(queryForm)))
  loadData()
}

const handleReset = () => {
  queryForm.order_no = ''
  queryForm.status = null
  handleSearch()
}

// 分页处理
const handleSizeChange = size => {
  pagination.size = size
  loadData()
}

const handlePageChange = page => {
  pagination.current = page
  loadData()
}

// 加载数据（模拟）
const loadData = () => {
  loading.value = true
  setTimeout(() => {
    // 模拟接口返回数据
    pagination.total = orderList.value.length
    loading.value = false
  }, 500)
}

// 初始化加载
loadData()
</script>

<style scoped>
.order-container {
  padding: 20px;
}

.query-card {
  margin-bottom: 20px;
}

.table-card {
  margin-top: 20px;
}

.product-list {
  padding: 10px 0;
}

.product-item {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
  &:last-child {
    border-bottom: none;
  }
}

.product-info {
  display: flex;
  align-items: center;
}

.product-image {
  width: 60px;
  height: 60px;
  border-radius: 4px;
}

.product-detail {
  margin-left: 10px;
  .product-name {
    font-weight: 500;
  }
  .product-spec {
    color: #666;
    font-size: 12px;
  }
  .product-quantity {
    color: #436a48;
    font-size: 14px;
  }
}

.pickup-info {
  font-size: 13px;
  color: #666;
  line-height: 1.6;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>